package cn.jinjimi.app.controller;

import android.content.Context;
import android.text.TextUtils;

import java.util.concurrent.Callable;

import bolts.Continuation;
import bolts.Task;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.entity.AppConfig.AppConfigs;
import cn.jinjimi.app.entity.AppConfig.AppModule;
import cn.jinjimi.app.entity.Version;
import cn.jinjimi.domain.ServiceManager;
import cn.jinjimi.domain.data.dto.BaseResDto;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.AppService;
import cn.jinjimi.domain.data.net.AuthService;
import cn.jinjimi.domain.store.ConfigStore;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 *
 * @author fuzhenhua
 * @date 2016/1/15
 */
public class AppConfigureController extends BaseController {

    /**
     * 配置列表
     *
     * @return
     */
    public static Task<AppConfigs> getConfig() {
        return Task.callInBackground(new Callable<AppConfigs>() {
            @Override
            public AppConfigs call() throws Exception {
                return ServiceManager.getService(ConfigStore.class).get();
            }
        });
    }

    /**
     * 根据url获取单独的配置项
     *
     * @param url
     * @return
     */
    public static Task<AppModule> getConfigModule(final String url) {
        return Task.callInBackground(new Callable<AppModule>() {
            @Override
            public AppModule call() throws Exception {
                return ServiceManager.getService(ConfigStore.class).get(url);
            }
        });
    }

    /**
     * 检查更新
     * Event CheckUpdateEvent
     * E Version
     *
     * @param version
     * @param channel
     */
    public static Task<Version> checkUpdate(final int version, final String channel) {
        return Task.callInBackground(new Callable<Version>() {
            @Override
            public Version call() throws Exception {
                Response<Version> response
                        = ServiceManager.getService(AppService.class)
                        .getVersion(version, channel).execute();

                if (response != null && response.isSuccessful()) {
                    return response.body();
                }
                return null;
            }
        });
    }

    /**
     * 启动统一接口
     *
     * @param context
     * @return
     */
    public static void start(Context context) {
        AccountController.checkLoginState();
    }
}
